
/**
 * 矛盾纠纷评估
 */

import { Button, Col, Drawer, Empty, Form, Input, Row, Select, Spin } from "antd"
import { memo, useEffect, useRef, useState } from "react"
import {
    PlusOutlined
} from '@ant-design/icons';
import styled from "@emotion/styled";
import { useAddBeefRisk, useGetBeefRiskBySearch, useUpdateBeefRiskById } from "../../../../service/beef/beef-risk-service";
import { useForm } from "antd/es/form/Form";
import { IUploadImgRefHandler, UploadImage } from "../../../../components/upload-img";
import { QueryObserverResult, RefetchOptions, RefetchQueryFilters } from "react-query";
import { IPage } from "../../../../types";
import { IBeef } from "../../../../service/beef/beef-service";
export type IBeefRiskType = {
    beefId: string | undefined;
    open: boolean;
    setOpen: (open: boolean) => void;
    refetch?: <TPageData>(options?: (RefetchOptions & RefetchQueryFilters<TPageData>) | undefined) => Promise<QueryObserverResult<IPage<IBeef>, unknown>>
}
const BeefRisk = ({ open, setOpen, beefId, refetch: refetchCallBack }: IBeefRiskType) => {
    const [form] = useForm();
    //图片附件的引用
    const imageRef = useRef<IUploadImgRefHandler>();
    const [showForm, setShowForm] = useState<boolean>(false)

    /**
   * !获取矛盾纠纷Hook
   */
    const { isLoading, data: dataSource } = useGetBeefRiskBySearch({ beefId: beefId || '' });
    /**
     * !获取风险评估Hook
     */
    const { mutateAsync: addMutation, isLoading: addIsLoading } = useAddBeefRisk();
    /**
     * !获取风险评估Hook
     */
    const { mutateAsync: updateMutation, isLoading: UIsLoading } = useUpdateBeefRiskById();
    useEffect(() => {
        if (dataSource) {
            setShowForm(true)
            setTimeout(() => {
                form.setFieldsValue({
                    riskLevel: dataSource.riskLevel,
                    userAge: dataSource.userAge,
                    homeMoney: dataSource.homeMoney,
                    major: dataSource.major,
                    userHealth: dataSource.userHealth,
                    isFLost: dataSource.isFLost,
                    isZsZcBl: dataSource.isZsZcBl,
                    isSolve: dataSource.isSolve
                })
                imageRef.current?.fill(dataSource.imgUrls || [])
            }, 200);

        } else {
            setShowForm(false)
            form.resetFields();
            imageRef.current?.rest();
        }
    }, [dataSource])
    /**
      * 更新方法
      */
    const updateHandler = async () => {
        //设置图片
        form.setFieldValue("imgUrls", imageRef.current?.getUrls());
        const values = await form.getFieldsValue();
        await updateMutation({ ...values, riskId: dataSource?.riskId, });
        refetchCallBack?.();
        setOpen(false);
        form.resetFields();
        imageRef.current?.rest();
    }

    /**
     * 保存方法
     */
    const saveHandler = async () => {
        //设置图片
        form.setFieldValue("imgUrls", imageRef.current?.getUrls());
        const isPass = await form.validateFields();
        if (!isPass) {
            return
        }
        const values = form.getFieldsValue();
        await addMutation({ beefId: beefId, ...values });
        refetchCallBack?.();
        setOpen(false);
        form.resetFields();
        imageRef.current?.rest();
    }

    return <Drawer open={open} title={"风险评估"} placement="bottom" height={'50rem'} closable={true} onClose={() => { setOpen(false) }} forceRender={true} footer={
        <>
            {
                dataSource ? (
                    <Button onClick={() => { updateHandler(); }}>
                        修改
                    </Button>
                ) : (
                    <Button onClick={() => { saveHandler(); }}>
                        保存
                    </Button>
                )
            }
        </>
    }>
        <BeefRiskHeader>
            {
                dataSource ? null : <Button style={{ float: 'right' }} type={'primary'} icon={<PlusOutlined />} onClick={() => { setShowForm(true) }} >
                    添加风险评估
                </Button>
            }
        </BeefRiskHeader>

        <Spin spinning={isLoading || addIsLoading || UIsLoading}>
            {
                dataSource || showForm ? null : <Empty description={'当前未进行风险评估'} />
            }
            {
                showForm ?
                    <Form form={form} labelCol={{ span: 7 }} wrapperCol={{ span: 17 }}>
                        <Row gutter={16}>
                            <Col span={8}>
                                <Form.Item label={'风险等级'} name={'riskLevel'} rules={[{ required: true, message: '风险等级不能为空' }]}>
                                    <Select options={[
                                        { value: 1, label: '一般' },
                                        { value: 2, label: '关注' },
                                        { value: 3, label: '重点' }
                                    ]} />
                                </Form.Item>
                            </Col>
                            <Col span={8}>
                                <Form.Item label={'年龄'} name={'userAge'} rules={[{ required: true, message: '年龄不能为空' }]}>
                                    <Input type="number" />
                                </Form.Item>
                            </Col>
                            <Col span={8}>
                                <Form.Item label={'家庭收入情况/年'} name={'homeMoney'}>
                                    <Input type="number" />
                                </Form.Item>
                            </Col>
                        </Row>
                        <Row gutter={16}>
                            <Col span={8}>
                                <Form.Item label={'是否重点人员'} name={'major'} rules={[{ required: true, message: '是否重点人员不能为空' }]}>
                                    <Select options={[
                                        {
                                            value: 0,
                                            label: '否',
                                        },
                                        {
                                            value: 1,
                                            label: '宣传煽动危害国家安全人员',
                                        },
                                        {
                                            value: 2,
                                            label: '扬言个人极端行为人员',
                                        },
                                        {
                                            value: 3,
                                            label: '社区矫正人员',
                                        },
                                        {
                                            value: 4,
                                            label: '刑满释放人员',
                                        },
                                        {
                                            value: 5,
                                            label: '取保候审人员',
                                        },
                                        {
                                            value: 6,
                                            label: '涉公安访人员',
                                        },
                                        {
                                            value: 7,
                                            label: '非访人员',
                                        },
                                        {
                                            value: 8,
                                            label: '吸毒人员',
                                        },
                                        {
                                            value: 9,
                                            label: '艾滋病人',
                                        },
                                        {
                                            value: 10,
                                            label: '肇事肇祸精神障碍患者',
                                        },
                                    ]} />
                                </Form.Item>
                            </Col>
                            <Col span={8}>
                                <Form.Item label={'身体状态'} name={'userHealth'} rules={[{ required: true, message: '身体状态不能为空' }]}>
                                    <Select options={[
                                        { value: 1, label: '健康' },
                                        { value: 2, label: '不健康' },
                                    ]} />
                                </Form.Item>
                            </Col>
                            <Col span={8}>
                                <Form.Item label={'是否五失人员'} name={'isFLost'} rules={[{ required: true, message: '是否五失人员不能为空' }]}>
                                    <Select options={[
                                        {
                                            value: 0,
                                            label: '否',
                                        },
                                        {
                                            value: 1,
                                            label: '投资失败',
                                        },
                                        {
                                            value: 2,
                                            label: '生活失意',
                                        },
                                        {
                                            value: 3,
                                            label: '心理失衡',
                                        },
                                        {
                                            value: 4,
                                            label: '关系失和',
                                        },
                                        {
                                            value: 5,
                                            label: '精神失常',
                                        },
                                    ]} />
                                </Form.Item>
                            </Col>
                        </Row>
                        <Row gutter={16}>
                            <Col span={8}>
                                <Form.Item label={'自杀、自残和其他暴力倾向'} name={'isZsZcBl'} rules={[{ required: true, message: '否存在自杀、自残和其他暴力倾向不能为空' }]}>
                                    <Select options={[
                                        { value: 1, label: '是' },
                                        { value: 2, label: '否' },
                                    ]} />
                                </Form.Item>
                            </Col>
                            <Col span={8}>
                                <Form.Item label={'是否久拖不决'} name={'isSolve'} rules={[{ required: true, message: '是否久拖不决不能为空' }]}>
                                    <Select options={[
                                        { value: 1, label: '是' },
                                        { value: 2, label: '否' },
                                    ]} />
                                </Form.Item>
                            </Col>
                            <Col span={8}>
                                <Form.Item label={'图片附件'} name={'imgUrls'} rules={[{ required: true, message: '图片地址数组不能为空' }]}>
                                    <UploadImage onRef={imageRef} maxCount={99} />
                                </Form.Item>
                            </Col>
                        </Row>
                    </Form> : null}
        </Spin>
    </Drawer >
}

export default memo(BeefRisk)

const BeefRiskHeader = styled.div`
    height: 4rem;
`